package com.xr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xr.pojo.Goods;
import com.xr.result.ResultMessages;
import com.xr.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static javafx.scene.input.KeyCode.R;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查询所有
    @GetMapping
    public ResultMessages getAll(){
        List<Goods> all = goodsService.getAll();
        return new ResultMessages(true,"查询成功",all);
    }

    //添加
    @PostMapping
    public ResultMessages save(@RequestBody Goods goods) {
        Boolean save = goodsService.save(goods);
        return new ResultMessages(save,"添加",null);
    }

    //修改
    @PutMapping
    public ResultMessages update(@RequestBody Goods goods) {
        Boolean update = goodsService.update(goods);
        return new ResultMessages(update,"查询成功",null);
    }

    //删除
    @DeleteMapping("{id}")
    public ResultMessages delete(@PathVariable Integer id) {
        Boolean delete = goodsService.delete(id);
        return new ResultMessages(delete,"查询成功",null);
    }

    //根据id查询
    @GetMapping("{id}")
    public ResultMessages getById(@PathVariable Integer id) {
        Goods byId = goodsService.getById(id);
        return new ResultMessages(true,"查询成功",byId);
    }

    //分页
    @GetMapping("{currentPage}/{pageSize}")
    public ResultMessages getPage(@PathVariable Integer currentPage, @PathVariable
            Integer pageSize ,Goods goods) {
        IPage<Goods> page = goodsService.getPage(currentPage, pageSize, goods);
        if( currentPage > page.getPages()) {
            page = goodsService.getPage((int)page.getPages(), pageSize, goods);
        }
        return new ResultMessages(true,"查询成功",page);
    }

}
